/**
 * PWM1 复用 GPIO[31]，位号J8_D1
 * PWM2 复用 GPIO[32]，位号J8_D2
 */

var pwm = require('pwm');

var pwm1 = pwm.open({
    id: 'pwm1',
    success: function () {
        console.log('pwm: open pwm success')
    },
    fail: function () {
        console.log('pwm: open pwm failed')
    }
});

var freq = pwm1.get().freq
var duty = pwm1.get().duty

console.log('pwm: pwm default config freq is ' + freq + ' duty is ' + duty)

duty = 0;
var cnt = 10;
var loop = 10;
var timer = setInterval(function () {
    if (duty >= 100) {
        duty = 0;
    }
    duty = duty + 20;

    pwm1.set({
        freq: 100,
        duty: duty
    })

    console.log('pwm: pwm test count ' + cnt)
    cnt = cnt - 1;
    if (cnt == 0) {
        pwm1.close();
        console.log('pwm: pwm test finish ')
        loop--;
        if (loop == 0) {
            clearInterval(timer);
        }
        else {
            pwm1 = pwm.open({
                id: 'pwm1',
                success: function () {
                    console.log('pwm: open pwm success')
                },
                fail: function () {
                    console.log('pwm: open pwm failed')
                }
            });
        }

        cnt = 10;
    }

}, 1000)
